package com.qiwx.pigeon.activity.home;

import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.andview.refreshview.XRefreshView;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.qiwx.pigeon.R;
import com.qiwx.pigeon.Utils.DBSqlUtil;
import com.qiwx.pigeon.Utils.MyToast;
import com.qiwx.pigeon.adapter.home.CageChangeAdapter;
import com.qiwx.pigeon.adapter.home.CageEmptyAdapter;
import com.qiwx.pigeon.app.AppContext;
import com.qiwx.pigeon.base.BaseActivity;
import com.qiwx.pigeon.data.CageStatusConfig;
import com.qiwx.pigeon.data.db.CageModel;
import com.qiwx.pigeon.data.params.CageParam;
import com.qiwx.pigeon.data.vo.CageBeanVo;
import com.qiwx.pigeon.data.vo.CageRemindBean;
import com.qiwx.pigeon.data.vo.WareHouseBean;
import com.qiwx.pigeon.data.vo.pageinfo.PageInfo;
import com.qiwx.pigeon.widget.ArrowView;
import com.qiwx.pigeon.widget.itemderection.LineItemDecoration;

import org.angmarch.views.NiceSpinner;
import org.litepal.LitePal;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @description
 * @Author qiwx
 * @Date 2020-04-06 11:30
 * 空笼
 **/
public class CageEmptyActivity extends BaseRemindActivity {

    List<CageBeanVo> data = new ArrayList<>();
    int current_pos = -1;
    CageParam cageParam;
//    @Override
//    public int getLayoutId() {
//        return R.layout.activity_cage_empty;
//    }

    @Override
    public void initAfterOnCreate() {
        super.initAfterOnCreate();
        cageParam=new CageParam();
        cageParam.setPageSize(pageSize);
        cageParam.setF_id(AppContext.getFarmId(this));
        initView();

//        wh_id = getIntent().getIntExtra("wh_id", 0);
        // xRefreshView.startRefresh();
        //loadData(true);

    }

    //查询总数
    public void queryAllAmount() {
        allCount = LitePal.where("wh_id=? and status=?", wh_id + "", CageStatusConfig.STATUS_KL + "").count(CageModel.class);
        txtAmount.setText(allCount + "");

    }
    @Override
    public void loadData(boolean isRefresh) {
        if (isRefresh) {
            pageNo = 1;
            data.clear();
           // queryAllAmount();
        } else {
            pageNo++;
        }

        this.isFresh = isRefresh;
        if (isRefresh) {
            pageNo = 1;
            data.clear();
        } else {
            pageNo++;
        }
        cageParam.setPageNum(pageNo);
        cageParam.setWh_id(wh_id);
        cageParam.setStatus(CageStatusConfig.STATUS_KL);
        mPresenter.queryCageEmptyByPage(cageParam);

//        int offset = (pageNo - 1) * pageSize;
////        String sql="select * from pigeonpairmodel where wh_id="+wh_id;
//        List<CageModel> list = LitePal
//                .limit(pageSize)
//                .offset(offset)
//                .where("wh_id=? and status=?", wh_id + "", CageStatusConfig.PRODUCE_KW + "").find(CageModel.class);
//        if (list != null && list.size() > 0) {
//            for (int i = 0; i < list.size(); i++) {
//                CageModel model = list.get(i);
//                CageRemindBean bean = new CageRemindBean();
//                bean.setC_id(model.getId());
//                bean.setC_number(model.getNumber());
//                bean.setStatus(model.getStatus());
//                data.add(bean);
//            }
//        }
//        if (isRefresh) {
//            xRefreshView.stopRefresh();
//        } else {
//            xRefreshView.stopLoadMore();
//        }
//        if (data.size() == 0) {
//            xRefreshView.setVisibility(View.GONE);
//            txtNodata.setVisibility(View.VISIBLE);
//        } else {
//            xRefreshView.setVisibility(View.VISIBLE);
//            txtNodata.setVisibility(View.GONE);
//            if (data.size() < allCount) {
//                xRefreshView.setPullLoadEnable(true);
//            } else {
//                if (!isRefresh) {
//                    MyToast.showLong("已经没有更多了");
//                }
//                xRefreshView.setPullLoadEnable(false);
//
//            }
//        }
//
//        adapter.notifyDataSetChanged();


    }

    @Override
    public BaseQuickAdapter getAdapter() {
        adapter = new CageEmptyAdapter(R.layout.adapter_remind_common_list_item, data);
        return adapter;
    }

    @Override
    public int getSize() {
        return data.size();
    }

    @Override
    public int addListViewHead() {
        return R.layout.cage_remind_kl_headview;
    }

    @Override
    public void initView() {
        super.initView();
        titleReturn.changeArrowLeft();
        titleContent.setText("空笼详情列表");
        txtAmountName.setText("空窝合计(个)：");
        if (adapter != null && adapter instanceof CageEmptyAdapter) {
            ((CageEmptyAdapter) adapter).setIPigeonPairInterface((pos, c_id, methodId) -> {
                setEmptyCage(pos, c_id);
            });
        }


    }

    public void setEmptyCage(int pos, int c_id) {
        current_pos=pos;
        CageParam cageParam=new CageParam();
        cageParam.setC_id(c_id);
        mPresenter.updateCageCL(cageParam);
//        CageModel model = LitePal.find(CageModel.class, c_id);
//        if (model != null) {
//            DBSqlUtil.setCageCL(model, false);
//            if (model.save()) {
//                MyToast.showLong("操作成功");
//                allCount = allCount - 1;
//                txtAmount.setText(allCount + "");
//                data.remove(pos);
//                adapter.notifyDataSetChanged();
//            }
//        } else {
//            MyToast.showLong("操作失败");
//
//        }
    }

    @Override
    public void onQueryCageSuccess(PageInfo<CageBeanVo> bean) {
        if (bean != null && bean.getList() != null) {
            data.addAll(bean.getList());
        }
        super.onQueryCageSuccess(bean);
    }

    @Override
    public void onUpdateCageStatusSuccess(int type) {
        if (current_pos >= 0) {
            allCount = allCount - 1;
            txtAmount.setText(allCount + "");
            data.remove(current_pos);
            adapter.notifyDataSetChanged();
            current_pos = -1;
        }

    }
}
